package com.yc.mvplib.Basis;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

import com.yc.mvplib.FastConfig;
import com.yc.mvplib.inter.IBasisView;
import com.yc.mvplib.manager.LoggerManager;
import com.yc.mvplib.utlis.ActivityStackUtil;
import com.yc.mvplib.utlis.FastUtil;
import com.yc.mvplib.utlis.StatusBarUtil;
import com.yc.mvplib.utlis.ToastUtil;
import com.yc.mvplib.utlis.UIHandler;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation_swipeback.SwipeBackActivity;

import static android.icu.lang.UCharacter.GraphemeClusterBreak.T;


/**
 * Created: AriesHoo on 2017/7/19 15:37
 * E-Mail: AriesHoo@126.com
 * Function: 所有Activity的基类
 * Description:
 */
    public abstract class BasisActivity  <P extends BasisPresenter, M extends BasisModel> extends SwipeBackActivity implements IBasisView {
    public P mPresenter;
    public M mModel;
    protected Activity mContext;
    protected View mContentView;
    protected Unbinder mUnBinder;
    protected boolean mIsViewLoaded = false;
    protected boolean mIsFirstShow = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        LoggerManager.i("====onCreate====" );
        //先判断xml没有设置屏幕模式避免将本身想设置的覆盖掉
        if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
            setRequestedOrientation(getOrientation());
        }
        super.onCreate(savedInstanceState);
        mContext = this;

        beforeSetContentView();
        initView(savedInstanceState);
    }

    @Override
    protected void onResume() {
        LoggerManager.i("====onResume====" );
        beforeLazyLoad();
        super.onResume();
    }

    @Override
    protected void onPause() {
        LoggerManager.i("====onPause====" );
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        LoggerManager.i("====onDestroy====" );

        super.onDestroy();
        if (mUnBinder != null) {
            mUnBinder.unbind();
        }
        ActivityStackUtil.getInstance().pop(this, false);
        if (mPresenter != null) mPresenter.detachVM();
    }

    /**
     * 是否开启滑动返回
     */
    protected void isSwipeBackEnable(boolean enable) {
         setSwipeBackEnable(enable);
    }
    @Override
    public int getContentBackground() {
        return FastConfig.getInstance(this).getContentViewBackgroundResource();
    }

    /**
     * 设置屏幕方向
     * 默认自动 ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
     * 竖屏 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
     * 横屏 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
     * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}
     *
     * @return
     */
    public int getOrientation() {
        return FastConfig.getInstance(this).getRequestedOrientation();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case FastConfig.REQUEST_CODE_SETTING:
                initView(new Bundle());
                break;
                default:
                    super.onActivityResult(requestCode, resultCode, data);
                    break;

        }

    }

    @Override
    public void beforeSetContentView() {
        LoggerManager.i("====beforeSetContentView====" );
        ActivityStackUtil.getInstance().push(this);
        mContentView = View.inflate(mContext, getContentLayout(), null);
        setContentView(mContentView);
        mUnBinder = ButterKnife.bind(this);
        mIsViewLoaded = true;
        beforeInitView();
    }

    @Override
    public void beforeInitView() {
        setStatusBarColor();
        if (getContentBackground() > 0) {
            mContentView.setBackgroundResource(getContentBackground());
        }
        mPresenter = FastUtil.getT(this, 0);
        mModel = FastUtil.getT(this, 1);
        isSwipeBackEnable(false);//默认关闭滑动推出
        if (this instanceof BasisView) mPresenter.attachVM(this, mModel);
    }
    public void setStatusBarColor() {
        StatusBarUtil.setTransparent(this);
    }
    /**
     * 加载数据
     */
    @Override
    public void loadData() {
        LoggerManager.i("====开始数据加载====" );
    }

    private void beforeLazyLoad() {
        if (!mIsViewLoaded) {//确保视图加载及视图绑定完成避免刷新UI抛出异常
            UIHandler.get().postDelayed(new Runnable() {
                @Override
                public void run() {
                    beforeLazyLoad();
                }
            },10);

        } else {
            lazyLoad();
        }
    }

    private void lazyLoad() {
        if (mIsFirstShow) {
            mIsFirstShow = false;
            loadData();
        }
    }

    /**
     * 跳转页面,无extra简易型
     *
     * @param tarActivity 目标页面
     */
    public void startActivity(Class<? extends Activity> tarActivity, Bundle options) {
        Intent intent = new Intent(this, tarActivity);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            startActivity(intent, options);
        } else {
            startActivity(intent);
        }
    }

    public void startActivity(Class<? extends Activity> tarActivity) {
        Intent intent = new Intent(this, tarActivity);
        startActivity(intent);
    }

    public void showToast(String msg) {
        ToastUtil.showShort(this,msg);
    }

    @Override
    public Context getContext() {
        return this.getApplicationContext();
    }
}
